Hệ thống quản lý nguồn nhân lực Dự án năm cuối trong c#
1 Public Class Frm_News
2
3 Dim news_id = 0
4
5 Sub formClear()
6
7 txtTitle.Clear()
8 txtDesc.Clear()
9
10 End Sub
11
12 Private Function autoId() As Integer ' generating auto news id
13
14 Dim query = "Select IsNull(Max(news_id+1),0) news_id From News"
15 Dim dr As SqlClient.SqlDataReader
16 dr = getDataReader(query)
17 dr.Read()
18 news_id = dr("news_id")
19 dr.Close()
20
21 Return news_id
22
23 End Function
24
25 Private Sub add()
26
27 dgvNews.Rows.Add()
28 dgvNews.Rows(dgvNews.RowCount - 1).Cells("NewsID").Value = txtID.Text
29 dgvNews.Rows(dgvNews.RowCount - 1).Cells("NewsTitle").Value = txtTitle.Text
30 dgvNews.Rows(dgvNews.RowCount - 1).Cells("NewsDesc").Value = txtDesc.Text
31 dgvNews.Rows(dgvNews.RowCount - 1).DefaultCellStyle.ForeColor = Color.Blue
32
33 formClear()
34
35 End Sub
36
37 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
38 Try
39 add()
40 Catch ex As Exception
41 MsgBox(ex.Message, MsgBoxStyle.Critical)
42 End Try
43 End Sub
44
45 Private Sub edit()
46
47 If dgvNews.SelectedRows.Count = 0 Then
48
49 Return
50
51 End If
52
53 If btnEdit.Text = "Edit" Then
54
55 txtID.Text = dgvNews.SelectedRows(0).Cells("NewsID").Value
56 txtTitle.Text = dgvNews.SelectedRows(0).Cells("NewsTitle").Value
57 txtDesc.Text = dgvNews.SelectedRows(0).Cells("NewsDesc").Value
58 dgvNews.Enabled = False
59 btnEdit.Text = "Update"
60
61 Else
62
63 dgvNews.SelectedRows(0).Cells("NewsID").Value = txtID.Text
64 dgvNews.SelectedRows(0).Cells("NewsTitle").Value = txtTitle.Text
65 dgvNews.SelectedRows(0).Cells("NewsDesc").Value = txtDesc.Text
66 dgvNews.Enabled = True
67 btnEdit.Text = "Edit"
68
69
70 End If
71
72 dgvNews.SelectedRows(0).DefaultCellStyle.ForeColor = Color.Blue
73
74 End Sub
75
76 Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
77 Try
78 edit()
79 Catch ex As Exception
80 MsgBox(ex.Message, MsgBoxStyle.Critical)
81 End Try
82 End Sub
83
84 Private Sub remove()
85
86 If dgvNews.SelectedRows.Count = 0 Then
87
88 Return
89
90 End If
91
92 dgvNews.SelectedRows(0).DefaultCellStyle.ForeColor = Color.Red
93
94 End Sub
95
96 Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
97 Try
98 remove()
99 Catch ex As Exception
100 MsgBox(ex.Message, MsgBoxStyle.Critical)
101 End Try
102 End Sub
103
104 Private Sub apply()
105
106 Dim query As String = String.Empty
107
108 For Each row As DataGridViewRow In dgvNews.Rows
109
110 If row.Cells("FromDB").Value = True Then
111
112 If row.DefaultCellStyle.ForeColor = Color.Black Then
113
114 Continue For
115
116 ElseIf row.DefaultCellStyle.ForeColor = Color.Blue Then
117
118 query = "Update News set news_title = '" & row.Cells("NewsTitle").Value & "',news_description = '" & row.Cells("NewsDesc").Value & "' Where news_id = '" & row.Cells("NewsID").Value & "'"
119
120 ElseIf row.DefaultCellStyle.ForeColor = Color.Red Then
121
122 query = "Delete From News Where news_id = " & row.Cells("NewsID").Value
123
124 End If
125
126
127 Else
128
129 If row.DefaultCellStyle.ForeColor = Color.Blue Then
130
131 query = "Insert Into News (news_title,news_description) Values ('" & row.Cells("NewsTitle").Value & "','" & row.Cells("NewsDesc").Value & "')"
132
133 ElseIf row.DefaultCellStyle.ForeColor = Color.Red Then
134
135 dgvNews.Rows.Remove(row)
136
137 Continue For
138
139 End If
140
141 End If
142
143 executeQuery(query)
144
145 Next
146
147 reload()
148
149 End Sub
150
151 Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
152 Try
153 apply()
154 Catch ex As Exception
155 MsgBox(ex.Message, MsgBoxStyle.Critical)
156 End Try
157 End Sub
158
159 Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
160 Try
161 apply()
162 Me.Close()
163 Catch ex As Exception
164 MsgBox(ex.Message, MsgBoxStyle.Critical)
165 End Try
166 End Sub
167
168 Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
169 Me.Close()
170 End Sub
171
172 Private Sub reload()
173
174 Dim query As String
175 query = "Select * From News"
176
177 dgvNews.Rows.Clear()
178
179 Dim dt As DataTable = getDataTable(query)
180
181 For Each row As DataRow In dt.Rows
182
183 dgvNews.Rows.Add()
184 dgvNews.Rows(dgvNews.RowCount - 1).Cells("NewsID").Value = row("news_id")
185 dgvNews.Rows(dgvNews.RowCount - 1).Cells("NewsTitle").Value = row("news_title")
186 dgvNews.Rows(dgvNews.RowCount - 1).Cells("NewsDesc").Value = row("news_description")
187 dgvNews.Rows(dgvNews.RowCount - 1).Cells("FromDB").Value = True
188 dgvNews.Rows(dgvNews.RowCount - 1).DefaultCellStyle.ForeColor = Color.Black
189
190 Next
191
192 txtID.Text = autoId()
193
194 End Sub
195
196 Private Sub Frm_News_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
197 Try
198 reload()
199 Catch ex As Exception
200 MsgBox(ex.Message, MsgBoxStyle.Critical)
201 End Try
202 End Sub
203 End Class